<?php
namespace Admin\Controller;

use Think\Controller;

class ArticleController extends CommonController
{

    private $article;
    private $category;

    public function __construct()
    {

        parent::__construct();
        $this->article = D("article");
        $this->category = D("category");


        $categories = $this->category->relation(true)->where("parent_id=0")->select();
        $this->assign("categories", $categories);


    }

    public function index()
    {


        $count = $this->article->count();// 查询满足要求的总记录数
        $Page = new \Think\Page2($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数(5)

        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $this->article->relation(true)->order('time desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->display();

        /*$articles=$this->article->relation(true)->select();
           //dump($articles);
        $this->assign("articles",$articles);
           $this->display();*/


    }

    public function destroy_checked()
    {
        //dump($_POST);
        $delete_id = I("post.delete_id");
        foreach ($delete_id as $id) {
            $this->article->delete($id);
        }
        $this->success("删除成功", U("index"));
    }

    public function create()
    {

        $this->display();
    }

    public function store()
    {

        //其他要做的
        /*
         * 封面栏目,不能添加文章
         *
         * 如果,当前栏目有子栏目, 则不能删除
         * 如果,当前栏目,没有子栏目,有文章, 也不能删除
         */

        //$file = array_filter($_POST['file']);

        // 编辑的时候,先将字符串拆分成数组explode('|', $string);  然后模板里foreach
        //$file = implode('|', $file);
        //dump($file);


        //exit;
        //exit;
        $this->article->create();
        $this->article->time = time();
        $this->article->add();

        $this->success("增加成功", U("index"));
    }

    public function del()
    {
        $id = I("get.id");
        $this->article->delete($id);
        $this->success("删除成功", U("index"));
    }

    public function edit()
    {

        $id = I("get.id");


        $article = $this->article->find($id);
        $file = $article["file"];//取出file
        $article["file"] = explode('|', $file);//定义成二维数组


        //dump($article);


        $this->assign("article", $article);


        $this->display();
    }

    public function do_edit()
    {
        //dump($_POST);
        $id = I("get.id");
        $this->article->create();
        $this->article->time = time();
        $this->article->where("id='$id'")->save();


        $this->success("修改成功", U("index"));
    }
}